﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling.Design;
using System.Collections;
using System.Windows.Forms.Design;

namespace Liekhus.Xaf.BusinessObjectDesigner.CustomCode.Editor
{
    public class TextAreaEditor : UITypeEditor
    {
        // This class is the list that will pop up when we click
        // on the down arrow of the mock-combo box in the designer.
        private class TextAreaControl : TextBox
        {
            public TextAreaControl(Component component, object value)
            {
                this.Multiline = true;
                this.Height = 125;
                this.Width = 300;
                this.WordWrap = false;
                this.Text = value.ToString();

                // Not setting the border to none just doesn't look good.
                this.BorderStyle = BorderStyle.None;
            }
        }

        /// <summary>
        /// Displays a list of available values for the specified component than sets the value.
        /// </summary>
        /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
        /// <param name="provider">A service provider object through which editing services may be obtained.</param>
        /// <param name="value">An instance of the value being edited.</param>
        /// <returns>The new value of the object. If the value of the object hasn't changed, this method should return the same object it was passed.</returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                // This service is in charge of popping our ListBox.
                IWindowsFormsEditorService service1 = ((IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)));

                if (service1 != null)
                {
                    // This is an internal Microsoft class representing the PropertyGrid entry for our component.
                    if (provider.GetType().FullName == "System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry")
                    {
                        // Get the component we're working on via reflection.
                        Component comp = ((Component)(provider.GetType().GetProperty("Component").GetGetMethod().Invoke(provider, new object[0])));

                        TextAreaControl control = new TextAreaControl(comp, value);

                        // Drop the list control.
                        service1.DropDownControl(control);

                        value = control.Text;
                        
                        // Close the list control after selection.
                        service1.CloseDropDown();
                    }
                }
            }

            return value;
        }

        /// <summary>
        /// Gets the editing style of the <see cref="EditValue"/> method.
        /// </summary>
        /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
        /// <returns>Returns the DropDown style, since this editor uses a drop down list.</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            // We're using a drop down style UITypeEditor.
            return UITypeEditorEditStyle.DropDown;
        }
    }
}
